﻿
//{{{ file documentation -------------------------------------------------------

/*!
 * @file     config.cpp
 * @brief    dicodor configuration class implementation
 *
 *           some more details
 */

//}}} --------------------------------------------------------------------------
//{{{ includes -----------------------------------------------------------------

// @(#) $Id: config.cpp 53 2012-05-23 13:56:41Z nsouchon-dico@altern.org $

#include "config.h"
#include "moc_config.cpp"

#include <QtCore/QDateTime>

//}}} --------------------------------------------------------------------------
//{{{ static variables ---------------------------------------------------------

// BASE
// NS
// NS_MERGED

const int Config::usedConf = NS_MERGED;

//}}} --------------------------------------------------------------------------
//{{{ Config -------------------------------------------------------------------
//}}} --------------------------------------------------------------------------
//{{{ .Config (QObject * parent) -----------------------------------------------

Config::Config (QObject * parent) :
    QSettings (parent)
{
}

//}}} --------------------------------------------------------------------------
//{{{ .const QStringList      Config::dicsPossiblePaths () ---------------------

const QStringList Config::dicsPossiblePaths ()
{
    if (usedConf == BASE)
    {
        return QStringList () 
            << appHomePath () + "dics/" 
            << "/usr/share/dicodor/" 
            << QApplication::applicationDirPath () + "/dics/" 
            << ":/dics/" 
            ;
    }
    if (usedConf == NS)
    {
        return QStringList () 
            << appHomePath () + "dics-ns/" 
            << "/usr/share/dicodor/ns/" 
            << QApplication::applicationDirPath () + "/dics/" 
            << ":/dics/" 
            ;
    }
    if (usedConf == NS_MERGED)
    {
        return QStringList () 
            << appHomePath () + "dics-ns-merged/" 
            << "/usr/share/dicodor/ns-merged/" 
            << QApplication::applicationDirPath () + "/dics/" 
            << ":/dics/" 
            ;
    }
}

//}}} --------------------------------------------------------------------------
//{{{ .void                   setOptionCurrentLanguage (const QString & lang) --

void Config::setOptionCurrentLanguage (const QString & lang)
{
    setValue ("optionCurrentLanguage", lang);
}

//}}} --------------------------------------------------------------------------
//{{{ .QString                optionCurrentLanguage () const -------------------

QString Config::optionCurrentLanguage () const
{
    return value ("optionCurrentLanguage", "fr").toString ();
}

//}}} --------------------------------------------------------------------------
//{{{ .int                    optionLengthMin () const -------------------------

int Config::optionLengthMin () const
{
    return value ("optionLengthMin", 2).toInt ();
}

//}}} --------------------------------------------------------------------------
//{{{ .void                   setOptionLengthMin (const int val) ---------------

void Config::setOptionLengthMin (const int val)
{
    setValue ("optionLengthMin", val);
}

//}}} --------------------------------------------------------------------------
//{{{ .void                   setOptionWeirdMax (const int val) ----------------

void Config::setOptionWeirdMax (const int val)
{
    setValue ("optionWeirdMax", val);
}

//}}} --------------------------------------------------------------------------
//{{{ .int                    optionWeirdMax () const --------------------------

int Config::optionWeirdMax () const
{
    return value ("optionWeirdMax", 2).toInt ();
}

//}}} --------------------------------------------------------------------------
//{{{ .void                   setOptionDistanceMax (const int val) -------------

void Config::setOptionDistanceMax (const int val)
{
    setValue ("optionDistanceMax", val);
}

//}}} --------------------------------------------------------------------------
//{{{ .int                    optionDistanceMax () const -----------------------

int Config::optionDistanceMax () const
{
    return value ("optionDistanceMax", 0).toInt ();
}

//}}} --------------------------------------------------------------------------
//{{{ Config::DicDesc ----------------------------------------------------------
//}}} --------------------------------------------------------------------------
//{{{ .const QList<DicDesc>   list () ------------------------------------------

//{{{ function documentation ---------------------------------------------------

/*!
 * @brief    dictionary descriptions hash
 *
 *           return dictionary descriptions as a hash\n
 *           whose key is the language\n
 *           and values are lists of dictionary description\n\n
 *           all found dictionaries are uniqually taken from the first directory where they appear
 *
 * @return   dictionary descriptions hash
 */

//}}} --------------------------------------------------------------------------

QHash<QString,QList<Config::DicDesc> > Config::DicDesc::list ()
{
    qDebug () << Q_FUNC_INFO;

    QHash<QString,QList<DicDesc> > res;
    QStringList                    pathes     = dicsPossiblePaths ();
    QString                        hashedPath = pathes[0] + "hashed/";
    QStringList                    dicFilters = QStringList () 
        << dicFilter ();

    QDir ().mkpath (hashedPath);

    foreach (const QString path, pathes)
    {
        QDir    dir (path);
        qDebug () << "   search in" << path;
        foreach (const QString & dicName, dir.entryList (dicFilters))
        {
            QFileInfo        info (path + dicName);
            QString          lang  = info.baseName ();
            QList<DicDesc> & list  = res[lang];
            bool             found = false;
            qDebug () << "     " << path << dicName << lang;

            // was a dictionary with same name already found ?

            for (int i = 0; i < list.count (); i++)
            {
                if (list[i].name == dicName)
                {
                    found = true;
                    break;
                }
            }

            // avoid duplicates

            if (! found)
            {
                DicDesc desc;
                desc.dir    = path;
                desc.name   = dicName;
                desc.hashed = hashedPath + dicName;
                list << desc;
            }
        }
    }

    foreach (QString l, res.keys ())
    {
        qDebug () << "   dics for" << l << "are :";
        foreach (DicDesc d, res[l])
        {
            qDebug () << "     " << d.name << d.dir << d.hashed;
        }
    }

    return (res);
}

//}}} --------------------------------------------------------------------------
//{{{ .bool                   hashNeedUpdate () --------------------------------

//{{{ function documentation ---------------------------------------------------

/*!
 * @brief    does a hashed dictionary need update ?
 *
 *           verify existence and modification date of a hashed dictionary
 *
 * @return   if a hashed dictionary need update
 */

//}}} --------------------------------------------------------------------------

bool Config::DicDesc::hashNeedUpdate ()
{
    QFileInfo hi (hashed);
    if (! hi.exists ()) return (true);

    QFileInfo di (dir + name);
    if (hi.lastModified () < di.lastModified ()) return (true);

    return (false);
}

//}}} --------------------------------------------------------------------------
//{{{ .QDebug                 operator << (QDebug & s, const DicDesc & d) ------

//{{{ function documentation ---------------------------------------------------

/*!
 * @brief    output a dictionary description for debug
 *
 * @return   the debug stream
 */

//}}} --------------------------------------------------------------------------

QDebug operator << (QDebug & s, const Config::DicDesc & d)
{
    s   << d.dir 
        << d.name 
        << d.hashed;
    return (s);
}

//}}} --------------------------------------------------------------------------

